<?php

namespace login\model;

require_once("LoginDAL.php");

class AddUserDAL {
	/**
	 * [AddUser description]
	 * @param string $username
	 * @param string $password 
	 * @param mysqli $mysqli
	 * @throws Exception om något går fel i databasen  
	 */
	public static function AddUser($username, $password, \mysqli $mysqli) {
		$sqlQ = "INSERT INTO `Users` (Username, PW) VALUES (?, ?)";

		if(LoginDAL::findUser($username, $mysqli)) {
			$stmt = $mysqli->prepare($sqlQ);
			if($stmt == false) {
				throw new \Exception("Prepare of sql question failed" . $mysqli->error);
			}

			$res = $stmt->bind_param("ss", $username, $password);
			if ($res == false) {
				throw new \Exception("Bind param failed " . $stmt->error);
			}

			$res = $stmt->execute();
			if($res == false) {
				throw new \Exception("Database execute failed " . $stmt->error);
			}
		} else {
			throw new \Exception("1"); // Användare finns redan.
		}
	}
}